#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2011 OpenWrt.org
# Copyright (C) 2015 GuoGuo <gch981213@gmail.com>
. /lib/functions.sh
START=96

SERVICE_USE_PID=1
SERVICE_WRITE_PID=1
SERVICE_DAEMONIZE=1

start() {
	[ -f /etc/config/dns2socks ] || {
		echo "You are missing configuration file /etc/config/dns2socks"
		return 1
	}

	config_load "dns2socks"

	config_get ENABLED config enabled

	[ "$ENABLED" = "0" ] && {
		echo "dns2socks is disabled."
		return 1
	}

	config_get SOCKSUSER config socks5_user
	config_get SOCKSPASS config socks5_pass
	config_get SOCKSSERVER config socks5_ip
	config_get SOCKSPORT config socks5_port
	config_get DNSSERVER config dns_ip
	config_get DNSPORT config dns_port
	config_get LOCALIP config local_ip
	config_get LOCALPORT config local_port
	service_start /usr/bin/dns2socks $SOCKSSERVER:$SOCKSPORT $DNSSERVER:${DNSPORT:=53} $LOCALIP:$LOCALPORT
}

stop() {
	service_stop /usr/bin/dns2socks
}
